﻿using Payroll.BusinessLogic.CustomerManager;
using Payroll.Common.Utils;
using PayrollHR.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PayrollHR.Administration.Modules
{
    public partial class CaseProfileSupportControl : BasePayrollAdministrationUserControl
    {
        public int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<ProfileProperty> customer = CustomerService.GetAllProfilePropertyByCustomerId(this.CustomerID);
                BindGrid(customer);
            }
        }
        protected void BindGrid(List<ProfileProperty> dt)
        {
            int i = dt.Count;
            if (i == 0)
                dt.Insert(i, new ProfileProperty { ProfilePropertyID = -1 });

            ViewState["CaseProfileSupport"] = dt;
            grvStudentDetails.DataSource = dt;
            grvStudentDetails.DataBind();
        }
        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            AddNewRow();
        }
        protected void AddNewRow()
        {
            List<ProfileProperty> listProfile = GetProfileProperty();
            listProfile.Insert(listProfile.Count, new ProfileProperty { ProfilePropertyID = -1 });

            BindGrid(listProfile);
        }
        public List<ProfileProperty> GetProfileProperty()
        {
            List<ProfileProperty> List = new List<ProfileProperty>();
            foreach (GridViewRow row in grvStudentDetails.Rows)
            {
                HiddenField hfProfilePropertyID = row.FindControl("hfProfilePropertyID") as HiddenField;
                int ProfilePropertyID = CommonHelper.ToInteger(hfProfilePropertyID.Value);
                TextBox txtPaperRequest = (TextBox)row.Cells[1].FindControl("txtPaperRequest");
                DatePicker dpOnRequest = (DatePicker)row.Cells[1].FindControl("dpOnRequest");
                DatePicker dpDateSubmitted = (DatePicker)row.Cells[1].FindControl("dpDateSubmitted");
                TextBox txtProTo = (TextBox)row.Cells[1].FindControl("txtProTo");
                DecimalTextBox txtPostage = (DecimalTextBox)row.Cells[1].FindControl("txtPostage");
                TextBox txtProComplete = (TextBox)row.Cells[1].FindControl("txtProComplete");
                TextBox txtNote = (TextBox)row.Cells[1].FindControl("txtNote");
                DatePicker dpFromdate = (DatePicker)row.Cells[1].FindControl("dpFromdate");
                DatePicker dpTodate = (DatePicker)row.Cells[1].FindControl("dpTodate");
                ProfileProperty profile = new ProfileProperty()
                {
                    ProfilePropertyID = ProfilePropertyID > 0 ? ProfilePropertyID : -1,
                    PaperRequest = txtPaperRequest.Text,
                    OnRequest = dpOnRequest.SelectText,
                    DateSubmitted = dpDateSubmitted.SelectText,
                    ProTo = txtProTo.Text.Trim(),
                    Postage = txtPostage.Value,
                    ProComplete = txtProComplete.Text.Trim(),
                    Note = txtNote.Text.Trim(),
                    UpdateByID = 1,
                    CreateByID = 1,
                    fromdate = dpFromdate.SelectText,
                    todate = dpTodate.SelectText,
                    CreatedOn = Convert.ToDateTime(DateTime.Today),
                    UpdatedOn = Convert.ToDateTime(DateTime.Today)
                };
                List.Add(profile);
            }
            return List;
        }

        protected void grvStudentDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                List<ProfileProperty> listProfile = GetProfileProperty();
                GridViewRow row = grvStudentDetails.Rows[e.RowIndex];
                List<int> List = new List<int>();
                HiddenField hfProfilePropertyID = row.FindControl("hfProfilePropertyID") as HiddenField;
                if (listProfile != null)
                {
                    listProfile.RemoveAt(row.RowIndex);
                    if (CommonHelper.ToInteger(hfProfilePropertyID.Value) > 0)
                    {
                        List.Add(CommonHelper.ToInteger(hfProfilePropertyID.Value));
                        this.CustomerService.DeleteProfileProperty(List);                        
                    }
                }
                BindGrid(listProfile);
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void grvStudentDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DatePicker dpOnRequest = (DatePicker)e.Row.FindControl("dpOnRequest");
                HiddenField hfOnRequest = (HiddenField)e.Row.FindControl("hfOnRequest") as HiddenField;
                dpOnRequest.SelectText = hfOnRequest.Value;

                DatePicker dpDateSubmitted = (DatePicker)e.Row.FindControl("dpDateSubmitted");
                HiddenField hfDateSubmitted = (HiddenField)e.Row.FindControl("hfDateSubmitted") as HiddenField;
                dpOnRequest.SelectText = hfDateSubmitted.Value;

                DecimalTextBox txtPostage = (DecimalTextBox)e.Row.FindControl("txtPostage");
                HiddenField hfPostage = (HiddenField)e.Row.FindControl("hfPostage") as HiddenField;
                txtPostage.Value = CommonHelper.ToDec(hfPostage.Value);

                DatePicker dpFromdate = (DatePicker)e.Row.FindControl("dpFromdate");
                HiddenField hfdpFromdate = (HiddenField)e.Row.FindControl("hfdpFromdate") as HiddenField;
                dpFromdate.SelectText = hfdpFromdate.Value;

                DatePicker dpTodate = (DatePicker)e.Row.FindControl("dpTodate");
                HiddenField hfdpTodate = (HiddenField)e.Row.FindControl("hfdpTodate") as HiddenField;
                dpTodate.SelectText = hfdpTodate.Value;

            }
        }
    }
}

